import Vue from 'vue'
import Router from 'vue-router'

import Layout from '@/views/layout'
import order from './modules/order'
Vue.use(Router)
/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
    {
        path: '/',
        name: 'index',
        redirect: '/home',
        component: Layout,
        meta: {
            hideInMenu: false,
            title: '工作台',
            affix: true,
            icon: 'el-icon-s-platform',
            roles: ['order_index'],
            notCache: true
        },
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('@/views/home.vue'),
                meta: {
                    hideInMenu: false,
                    notCache: false,
                    affix: true,
                    title: '工作台',
                    roles: ['order_index'],
                    icon: 'el-icon-s-platform'
                }
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login.vue'),
        meta: {
            hideInMenu: true,
            title: '登录'
        }
    },
    {
        path: '/demoui',
        name: 'demoui',
        redirect: '/demouiabc',
        component: Layout,
        meta: {
            hideInMenu: true,
            title: 'demoUI规范',
            icon: 'el-icon-s-platform'
        },
        children: [{
            path: '/demouiabc',
            name: 'demouiabc',
            component: () => import('@/views/demoUi.vue'),
            meta: {
                hideInMenu: true,
                title: 'UI规范demo',
                icon: 'el-icon-s-platform'
            }
        }]
    },
    {
        name: 'download',
        path: '/download',
        component: () => import('@/views/download.vue'),
        meta: {
            hideInMenu: true,
            title: '浏览器下载'
        }
    }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
    ...order,
    {
        name: '404',
        path: '*',
        redirect: '/404',
        component: () => import('@/views/error.vue'),
        meta: {
            hideInMenu: true,
            title: '哎呀，打不开网页了呢~',
            roles: ['order_center']
        }
    },
    // 404 page must be placed at the end !!!
    {
        name: 'error_404',
        path: '/404',
        component: () => import('@/views/error.vue'),
        meta: {
            hideInMenu: true,
            title: '哎呀，打不开网页了呢~',
            roles: ['order_center']
        }
    },
    {
        name: 'error_500',
        path: '/error',
        component: () => import('@/views/error.vue'),
        meta: {
            hideInMenu: true,
            title: '哎呀，服务器出了点小问题~',
            roles: ['order_center']
        }
    }
]
/**
 * 重写路由的push方法
 */
const routerPush = Router.prototype.push
Router.prototype.push = function push (location) {
    return routerPush.call(this, location).catch(error => error)
}
const createRouter = () => new Router({
    // mode: 'history', // require service support
    // mode: 'hash',
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
    base: process.env.BASE_URL
})

const router = createRouter()

export function resetRouter () {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}
export default router
